package com.zz.ecommerce.togatherDeclareMgr.qdc;

import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.common.bean.Result;
import com.zz.ecommerce.common.bean.qdc.db.TDeclformHeadQdc;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.TDeclformHeadQdcDao;
import com.zz.ecommerce.togatherDeclareMgr.qdc.service.QDCustDeclarationGenerateService;
import com.zz.ecommerce.utils.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 报关单生成
 * <p/>
 * 针对青岛企业上报的申报清单进行生成
 * <p/>
 * Created by liuguo.rocky on 2016/4/6.
 */
@Controller
@RequestMapping("/togatherDeclareMgr/qdcDeclarationGenerate")
public class QDCustDeclarationGenerateController {


    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private QDCustDeclarationGenerateService qdCustDeclarationGenerateService;

    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    /**
     * 申报清单管理进口B2C(保税)
     *
     * @return
     */
    @RequestMapping(value = "declarationBaoshuiForm", method = {RequestMethod.GET})
    public String declarationBaoshuiForm() {
        return "/togatherDeclareMgr/qdcDeclarationGenerate/baoshui/declaration_form";
    }

    /**
     * 申报清单管理进口B2C(直购)
     *
     * @return
     */
    @RequestMapping(value = "declarationZhigouForm", method = {RequestMethod.GET})
    public String declarationZhigouForm() {
        return "/togatherDeclareMgr/qdcDeclarationGenerate/zhigou/declaration_form";
    }

    /**
     * 申报清单管理出口B2C
     *
     * @return
     */
    @RequestMapping(value = "declExportB2CForm", method = {RequestMethod.GET})
    public String declarationExportB2CForm() {
        return "/togatherDeclareMgr/qdcDeclarationGenerate/exportb2c/declaration_form";
    }

    /**
     * 查询申报清单管理列表
     *
     * @param tDeclformHeadQdc
     * @param request
     * @return
     */
    @RequestMapping("/queryDeclareList")
    @ResponseBody
    public GridResult queryDeclareList(TDeclformHeadQdc tDeclformHeadQdc, GridRequest request, HttpSession session) {
        tDeclformHeadQdc.setOrgId(SessionUtil.getOrganizationId(session));
        List<TDeclformHeadQdc> list = tDeclformHeadQdcDao.pageWhere(TableConstants.T_DECLFORM_HEAD_QDC,
                                                                    tDeclformHeadQdc,
                                                                    request.getStart(), request.getEnd(),
                                                                    request.getSortOrder(), " and RELATE_DECL_CUST_ID" +
                                                                                            " is null");
        long count = tDeclformHeadQdcDao.countWhere(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc, " and RELATE_DECL_CUST_ID" +
                                                                                                    " is null");

        return new GridResult(list, request, count);
    }

    /**
     * 申报单申报操作
     *
     * @param idList
     * @return
     */
    @RequestMapping("/declare")
    @ResponseBody
    public Result declare(@RequestParam("id") List<String> idList) {
        Result result = new Result();
        result.setCode("0");
        try {
            qdCustDeclarationGenerateService.toDeclareCust(idList);
        } catch (Exception e) {
            String msg = e.getMessage() + "";
            logger.error(msg, e);
            result.setCode("1");
            result.setMsg(msg);
        }

        return result;
    }
}
